<style>
.meanInfo .el-input.is-disabled .el-input__inner{
  color:black;
}
.meanInfo .el-textarea.is-disabled .el-textarea__inner{
  color:black;
}
</style><template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="语言" prop="langueType">
        <el-select v-model="queryParams.langueType" placeholder="语言" clearable size="small">
          <el-option
            v-for="dict in langueTypeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="协议类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="协议编号" prop="meanNo">
        <el-input
          v-model="queryParams.meanNo"
          placeholder="请输入协议编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="协议名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入协议名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['/mean/insert']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['/mean/updateById']"
        >修改</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="语言类型" align="center" prop="type" :formatter="langueTypeFormat" width="100" />
      <el-table-column label="协议类型" align="center" prop="type" :formatter="typeFormat" width="100" />
      <el-table-column label="协议编号" align="center" prop="meanNo" />
      <el-table-column label="协议名称" align="center" prop="name" />
      <el-table-column label="协议版本" align="center" prop="version" />
      <el-table-column label="创建日期" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['/mean/updateById']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewContent(scope.row)"
            v-hasPermi="['/meanContent/listByDto']"
          >修改内容</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/mean/deleteByMeanNo']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改协议信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="语言" prop="langueType">
          <el-select v-model="form.langueType" placeholder="请选择">
            <el-option
              v-for="dict in langueTypeOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="协议名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入协议名称" />
        </el-form-item>
        <el-form-item label="协议版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入协议版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改协议内容对话框 -->
    <el-dialog :title="title" :visible.sync="openContent" v-if="openContent" width="500px" append-to-body>
      <div v-html="formContent.content" v-if="contentViewOrUpdate == eContentViewOrUpdate.VIEW.code"></div>
      <el-form ref="formContent" :model="formContent" :rules="rulesContent" label-width="80px" v-if="contentViewOrUpdate == eContentViewOrUpdate.UPDATE.code">
        <el-form-item label="语言" prop="content">
          <editor v-model="formContent.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="顺序" prop="sortNum">
          <el-input v-model="formContent.sortNum" placeholder="请输入协议顺序" />
        </el-form-item>
      </el-form>
      <pagination
        layout="prev, pager, next, jumper"
        :total="totalContent"
        :page-size="1"
        :page.sync="queryParamsContentPage.pageNum"
        :limit.sync="queryParamsContentPage.pageSize"
        @pagination="contentPage"
        v-if="contentList && contentList.length > 0"
      />
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="handleAddContent"
          v-if="contentList && contentViewOrUpdate == eContentViewOrUpdate.VIEW.code"
          v-hasPermi="['/meanContent/insert']"
        >添 加</el-button>
        <el-button
          type="primary"
          @click="handleUpdateContent"
          v-if="contentList && contentList.length > 0 && contentViewOrUpdate == eContentViewOrUpdate.VIEW.code"
          v-hasPermi="['/meanContent/updateById']"
        >修 改</el-button>
        <el-button
          type="primary"
          @click="submitFormContent"
          v-if="contentViewOrUpdate == eContentViewOrUpdate.UPDATE.code"
        >保 存</el-button>
        <el-button
          type="primary"
          @click="handleDeleteContent"
          v-if="contentList && contentList.length > 0 && formContent.id"
          v-hasPermi="['/meanContent/deleteById']"
        >删 除</el-button>
        <el-button @click="cancelContent">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/mean/info";
import { listInfo as listContent, delInfo as delContent, addInfo as addContent, updateInfo as updateContent } from "@/api/mean/content";
import Editor from '@/components/Editor';
import { LANGUE_TYPE, PAGE_SIZE } from "@/constant/constant";

export default {
  name: "Info",
  components: {
    Editor,
  },
  data() {
    return {
      // 枚举属性
      // 内容弹出层是显示还是修改(1:显示,2:修改)
      eContentViewOrUpdate: {
        VIEW:{
            code: 1,
            name: "显示"
        },
        UPDATE:{
            code: 2,
            name: "修改"
        }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      meanNos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内容总条数
      totalContent: 0,
      // 协议信息表格数据
      infoList: [],
      // 协议内容表格数据
      contentList: [],
      // 协议类型字典
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openContent: false,
      // 内容弹出层是显示还是修改(1:显示,2:修改)
      contentViewOrUpdate: 1,
      // 协议类型数据字典
      typeOptions: [],
      // 语言类型数据字典
      langueTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: PAGE_SIZE,
        langueType: LANGUE_TYPE,
        type: null,
        meanNo: null,
        name: null
      },
      // 内容查询参数
      queryParamsContent: {
        meanNo: null
      },
      // 内容分页参数
      queryParamsContentPage: {
        lastPageNum: 1,
        pageNum: 1,
        pageSize: 1
      },
      // 表单参数
      form: {},
      // 内容表单参数
      formContent: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "协议名称不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "协议版本不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "协议类型不能为空", trigger: "blur" }
        ],
        langueType: [
          { required: true, message: "语言类型不能为空", trigger: "blur" }
        ]
      },
      // 内容表单校验
      rulesContent: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();	
    this.getDicts("mean_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("langue_type").then(response => {
      response.data.forEach(element => {
        element.val = parseInt(element.val)
      });
      this.langueTypeOptions = response.data;
    });
    this.getDicts("mean_type").then(response => {
      response.data.forEach(element => {
        element.val = parseInt(element.val)
      });
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询协议信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.data;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        meanNo: null,
        userNo: null,
        name: null,
        version: null,
        type: null,
        langueType: this.queryParams.langueType,
        version: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.meanNos = selection.map(item => item.meanNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加协议信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改协议信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const meanNos = row.meanNo || this.meanNos
      this.$confirm('是否确认删除协议编号为"' + meanNos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(meanNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },






    
    /** 
     * 查询协议内容列表
     * viewOrAdd 显示还是添加(true:显示,false:添加)
     */
    getListContent(row,viewOrAdd) {
      const id = row.id;
      const meanNo = row.meanNo;
      this.loading = true;
      listContent(this.queryParamsContent).then(response => {
        this.contentList = response.data;
        this.totalContent = response.page.total;
        // 如果协议不存在就重置表单数据
        if(this.contentList.length < 1){
          this.resetContent();
          this.formContent.meanNo = meanNo;
        } else {
          // 标记是否存在之前的数据
          var isExist = false;
          // 如果协议存在就显示之前的数据
          for(var i=0; i<this.contentList.length; i++){
            const content = this.contentList[i];
            if(id && content.id == id){
              this.formContent = content;
              this.queryParamsContentPage.pageNum = i+1;
              isExist = true;
            }
          }
          if(!isExist){
            // 跳转到添加的协议
            for(var i=0; i<this.contentList.length; i++){
              const content = this.contentList[i];
              if(id && content.id == id){
                this.formContent = content;
                this.queryParamsContentPage.pageNum = i+1;
                isExist = true;
              }
            }
            const pageNum = viewOrAdd?1:this.contentList.length;
            this.queryParamsContentPage.pageNum = pageNum;
            this.formContent = this.contentList[pageNum-1];
          }
        }
        this.loading = false;
      });
    },
    // 协议内容取消按钮
    cancelContent() {
      this.openContent = false;
      this.resetContent();
    },
    /** 协议内容分页 */
    contentPage() {
      if(this.contentViewOrUpdate == this.eContentViewOrUpdate.UPDATE.code){
        this.$confirm('内容未保存,如果离开内容将消失', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if(!this.contentList[this.contentList.length-1].id){
              this.contentList.length = this.contentList.length-1;
            }
            this.contentViewOrUpdate = this.eContentViewOrUpdate.VIEW.code;
            this.queryParamsContentPage.lastPageNum = this.queryParamsContentPage.pageNum;
            this.refreshPage();
          }).catch(() => {
            this.queryParamsContentPage.pageNum = this.queryParamsContentPage.lastPageNum;
          })
      } else {
        this.queryParamsContentPage.lastPageNum = this.queryParamsContentPage.pageNum;
        this.refreshPage();
      }
    },
    /** 刷新分页数据 */
    refreshPage() {
      this.loading = true;
      const meanNo = this.formContent.meanNo;
      this.totalContent = this.contentList.length;
      this.formContent = this.contentList[this.queryParamsContentPage.pageNum - 1];
      this.formContent.meanNo = meanNo;
      this.loading = false;
    },
    // 内容表单重置
    resetContent() {
      this.formContent = {
        id: null,
        meanNo: null,
        content: null,
        sortNum: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("formContent");
      this.contentList = [];
      this.contentViewOrUpdate = this.eContentViewOrUpdate.VIEW.code;
    },
    /**显示内容按钮操作 */
    handleViewContent(row) {
      // 显示第一页数据
      this.queryParamsContentPage.pageNum = 1;
      // 设置查询参数
      this.queryParamsContent.meanNo = row.meanNo;
      // 显示数据
      this.contentViewOrUpdate = this.eContentViewOrUpdate.VIEW.code;
      // 查询协议数据
      this.getListContent(row,true);
      // 打开弹出层
      this.openContent = true;
      this.title = "协议内容信息";
    },
    /** 新增内容按钮操作 */
    handleAddContent() {
      this.contentList.push({});
      this.queryParamsContentPage.pageNum = this.contentList.length;
      this.contentPage();
      this.contentViewOrUpdate = this.eContentViewOrUpdate.UPDATE.code;
    },
    /** 修改内容按钮操作 */
    handleUpdateContent(row) {
      this.contentViewOrUpdate = this.eContentViewOrUpdate.UPDATE.code;
    },
    /** 删除按钮操作 */
    handleDeleteContent() {
      const ids = this.formContent.id;
      this.$confirm('是否确认删除协议内容编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContent(ids);
        }).then(() => {
          this.getListContent(this.formContent,true);
          this.contentViewOrUpdate = this.eContentViewOrUpdate.VIEW.code;
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 内容提交按钮 */
    submitFormContent() {
      this.$refs["formContent"].validate(valid => {
        if (valid) {
          if (this.formContent.id != null) {
            updateContent(this.formContent).then(response => {
              this.msgSuccess("修改成功");
              this.getListContent(this.formContent,true);
            });
          } else {
            addContent(this.formContent).then(response => {
              this.msgSuccess("新增成功");
              this.formContent.id = response.data;
              this.getListContent(this.formContent,false);
            });
          }
          this.contentViewOrUpdate = this.eContentViewOrUpdate.VIEW.code;
        }
      });
    },
    // 协议类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 语言类型字典翻译
    langueTypeFormat(row, column) {
      return this.selectDictLabel(this.langueTypeOptions, row.langueType);
    },
  }
};
</script>
